package com.zyq.exception.handler;

import com.zyq.exception.ServiceException;
import com.zyq.web.JsonResult;
import com.zyq.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult<Void> handleServiceException(ServiceException e){
        log.debug("GlobalExceptionHandler---->handleServiceException()::捕获到ServiceException：{}", e.getMessage());
        return JsonResult.fail(e.getServiceCode(), e.getMessage());
    }

    @ExceptionHandler
    public JsonResult<Void> handleThrowable(Throwable e){
        log.debug("捕获到Throwable：{}", e.getMessage());
        e.printStackTrace(); // 强烈建议
        String message = "程序运行时出现未知错误，请联系系统管理员！";
        return JsonResult.fail(ServiceCode.ERR_UNKNOWN,message);
    }
}
